package com.xoa.service.party;

import com.xoa.dao.party.SafeConductActivityMapper;
import com.xoa.model.enclosure.Attachment;
import com.xoa.model.party.SafeConductActivity;
import com.xoa.util.ExcelUtil;
import com.xoa.util.GetAttachmentListUtil;
import com.xoa.util.common.log.FileUtils;
import com.xoa.util.common.wrapper.BaseWrapper;
import com.xoa.util.page.PageParams;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 程叶同 on 2018/1/11.
 */
@Service
public class SafeActivityService {
    @Autowired
    SafeConductActivityMapper safeConductActivityMapper;

    public BaseWrapper addActivity(SafeConductActivity safeConductActivity){
        BaseWrapper baseWrapper=new BaseWrapper();
        if(safeConductActivity!=null){
            int in = safeConductActivityMapper.insert(safeConductActivity);
            if(in>0){
                baseWrapper.setStatus(true);
                baseWrapper.setFlag(true);
                baseWrapper.setMsg("ok");
            }
        }
        return  baseWrapper;
    }


    /**
    * @Author 程叶同
    * @Description删除
    * @Date 10:23 2018/1/12
    */
    public BaseWrapper deleteActivity(Integer id){
        BaseWrapper baseWrapper=new BaseWrapper();
        if(id!=null){
            int i = safeConductActivityMapper.deleteByPrimaryKey(id);
            if(i>0){
                baseWrapper.setMsg("修改成功");
                baseWrapper.setStatus(true);
                baseWrapper.setFlag(true);
            }
        }
        return baseWrapper;
    }


    /**
     * @Author 程叶同
     * @Description 修改记录
     * @Date 14:03 2018/1/10
     */

    public BaseWrapper updateActivity(SafeConductActivity safeConductActivity){
        BaseWrapper baseWrapper=new BaseWrapper();
        if(safeConductActivity!=null){
            int i = safeConductActivityMapper.updateByPrimaryKey(safeConductActivity);
            if(i>0){
                baseWrapper.setMsg("修改成功");
                baseWrapper.setStatus(true);
                baseWrapper.setFlag(true);
            }
        }
        return baseWrapper;
    }
    /**
     * @Author 程叶同
     * @Description 通过id查询
     * @Date 14:10 2018/1/10
     */

    public BaseWrapper getActivitysById(HttpServletRequest request, Integer id){
        BaseWrapper baseWrapper=new BaseWrapper();
        String sqlType = "xoa"
                + (String) request.getSession().getAttribute("loginDateSouse");
        if(id!=null&&id!=0){
            SafeConductActivity safeConductActivity = safeConductActivityMapper.selectByPrimaryKey(id);
            List<Attachment> attachments = GetAttachmentListUtil.returnAttachment(safeConductActivity.getAttachmentName(), safeConductActivity.getAttachmentId(), sqlType, GetAttachmentListUtil.MODULE_parameter);
            safeConductActivity.setAttachmentList(attachments);
            if(safeConductActivity!=null){
                baseWrapper.setData(safeConductActivity);
                baseWrapper.setMsg("查询成功");
                baseWrapper.setFlag(true);
                baseWrapper.setStatus(true);
            }
        }
        return  baseWrapper;
    }

    /**
     * @Author 程叶同
     * @Description 查询全部
     * @Date 14:10 2018/1/10
     */
    public BaseWrapper getList(HttpServletRequest request, Integer page, Integer pageSize, String startTime, String endTime, String reportUser){
        String sqlType = "xoa"
                + (String) request.getSession().getAttribute("loginDateSouse");
        BaseWrapper baseWrapper=new BaseWrapper();
        Map<String,Object> map=new HashMap<String,Object>();
        Map<String,Object> map1=new HashMap<String,Object>();
        PageParams pageParams=new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        map.put("page",page-1);
        map.put("pageParams",pageParams);
        map.put("startTime",startTime);
        map.put("endTIme",endTime);
        map.put("reportUser",reportUser);
        map1.put("reportUser",reportUser);
        map1.put("startTime",startTime);
        map1.put("endTIme",endTime);
        List<SafeConductActivity> safeConductActivities = safeConductActivityMapper.selectList(map);
        int i = safeConductActivityMapper.totalItems(map1);
        if(safeConductActivities!=null){
            for(SafeConductActivity vr:safeConductActivities){
                List<Attachment> attachments = GetAttachmentListUtil.returnAttachment(vr.getAttachmentName(), vr.getAttachmentId(), sqlType, GetAttachmentListUtil.MODULE_parameter);
                vr.setAttachmentList(attachments);
            }
            baseWrapper.setStatus(true);
            baseWrapper.setData(safeConductActivities);
            baseWrapper.setFlag(true);
            baseWrapper.setMsg("ok");
            baseWrapper.setTotle(i);
        }
        return  baseWrapper;
    }

    /**
    * @Author 程叶同
    * @Description 导出
    * @Date 15:11 2018/1/16
    */
    public BaseWrapper fileOut(HttpServletRequest request, HttpServletResponse response, String ids){
        BaseWrapper baseWrapper=new BaseWrapper();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd Hh:mm:ss");
        List<SafeConductActivity> safeConductActivities=null;
        int count=1;
        if(ids!=null&&ids!=""){
            String[] id=ids.split(",");
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("id",id);
            safeConductActivities=safeConductActivityMapper.selectList(map);
        }
        if(safeConductActivities!=null){
            for(SafeConductActivity sa:safeConductActivities){
                sa.setTimestr(sdf.format(sa.getTime()));
                sa.setAid(count);
                Integer type=Integer.parseInt(sa.getType());
                switch (type){
                    case 0:
                        sa.setType("形式");
                        break;
                    case 1:
                        sa.setType("讲座");
                        break;
                    case 2:
                        sa.setType("培训");
                        break;
                    case 3:
                        sa.setType("宣传活动");
                        break;
                    case 4:
                        sa.setType("演练");
                        break;
                }
                count++;
            }
        }
        try {

            HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("安全宣传活动", 9);
            String[] secondTitles = {"序号","时间","形式","地点","培训对象","上报人姓名","活动信息"};
            HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);

            // String[] beanProperty = {user.getDep().getDeptName(),"userName","userPrivName", "roleAuxiliaryName","online","sex","online","telNoDept","telNoDept","departmentPhone","email"};
            String[] beanProperty = {"aid","timestr","type","location","trainingObject","reportUser","activityInfo"};

            HSSFWorkbook workbook = ExcelUtil.exportExcelData(workbook2, safeConductActivities, beanProperty);
            ServletOutputStream out = response.getOutputStream();

            String filename = "安全宣传活动.xls";
            filename = FileUtils.encodeDownloadFilename(filename,
                    request.getHeader("user-agent"));
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-disposition",
                    "attachment;filename=" + filename);
            workbook.write(out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return  baseWrapper;
    }

}
